/*
	* Created by : Varun Tyagi
	*
	* Time for rendering text. 
	* SDL does not support *.ttf files natively so the SDL_ttf extension library in needed.
	* SDL ttf is an extension library that allows us to generate surface from true type fonts.
*/

/* Include the header files. */
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>

/* Constant screen attribute. */
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

/* Common surface in the game.*/
SDL_Surface *pBackground = nullptr;
SDL_Surface *pMessage = nullptr;
SDL_Surface *pScreen = nullptr;

/* The event structure. */
SDL_Event event;

/* The font that we are going to use. */
TTF_Font *pFont = nullptr;

/* The color of the font. */
SDL_Color textColor = { 255, 255, 255 };

/* Image Loader. */
SDL_Surface *load_Image( std::string filename )
{
	/* The image that is being loaded. */
	SDL_Surface *pLoadedImage = nullptr;

	/* The optimized version of the loaded image. */
	SDL_Surface *pOptimizedImage = nullptr;

	/* Load the image. */
	pLoadedImage = IMG_Load( filename.c_str() );

	/* If we have correctly loaded the image. */
	if( nullptr != pLoadedImage )
	{
		/* Create an optimized image. */
		pOptimizedImage = SDL_DisplayFormat( pLoadedImage );

		/* Free the old unoptimized image. */
		SDL_FreeSurface( pLoadedImage );

		/* If the surface is optimized. */
		if( nullptr != pOptimizedImage )
		{
			/* Color key Surface. */
			SDL_SetColorKey( pOptimizedImage,  SDL_SRCCOLORKEY, SDL_MapRGB( pOptimizedImage->format, 0, 0xFF, 0xFf ) );
		}
	}

	/* Return the optimized image. */
	return pOptimizedImage;
}

/* Apply the image. */
void apply_image( int x, int y, SDL_Surface *pSource, SDL_Surface *pDestination, SDL_Rect *clip = NULL )
{
	/* Holds offset. */
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	/* Blit. */
	SDL_BlitSurface( pSource, clip, pDestination, &offset );
}

/* Init SDL. */
bool init_SDL( void )
{
	/* Init everything. */
	if( SDL_Init(SDL_INIT_EVERYTHING) == -1 )
	{
		/* couldn't load SDL. */
		return false;
	}

	/* Setup the screen. */
	pScreen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

	/* Check if we have correctly loaded the screen. */
	if( nullptr == pScreen )
	{
		return false;
	}

	/* Initialize the font. */
	if( TTF_Init() == -1 )
	{
		return false;
	}

	/* Set the window caption. */
	SDL_WM_SetCaption( "Font Loader", nullptr );

	/* If everything went well, return true. */
	return true;
}

/* Files loader. */
bool load_files( void )
{
	/* Load the background image. */
	pBackground = load_Image( "background.png" );

	/* Open the font. */
	pFont = TTF_OpenFont( "lazy.ttf", 28 );

	/* Check to see if there is a problem loading the background image. */
	if( nullptr == pBackground )
	{
		/* QUIT. */
		return false;
	}

	/* Check to see if there is a problem loading the font file. */
	if( nullptr == pFont )
	{
		/* QUIT. */
		return false;
	}

	/* Return true if everything went on fine. */
	return true;
}

/* Cleaner function. */
void clean_up( void )
{
	/* Free all the surfaces. */
	SDL_FreeSurface( pBackground );
	SDL_FreeSurface( pMessage );

	/* Close the font that was loaded earlier. */
	TTF_CloseFont( pFont );

	/* QUIT SDL_ttf. */
	TTF_Quit();

	/* QUIT SDL. */
	SDL_Quit();
}

/* Main function. */
int main( int argc, char** argv )
{
	/* QUIT flag. */
	bool bQuit = false;

	/* Initialize. */
	if( false == init_SDL() )
	{
		/* QUIT. */
		return 1;
	}

	/* Load the files. */
	if( false == load_files() )
	{
		/* QUIT. */
		return 1;
	}

	/* Render the text. */
	pMessage = TTF_RenderText_Solid( pFont, "I think I am awesome, if you think I am not, then for sure I am awesome.", textColor );

	/* Check to see if there was any error rendering the text. */
	if( nullptr == pMessage )
	{
		/* QUIT. */
		return 1;
	}

	/* Now apply the images to the screen. */
	apply_image( 0, 0, pBackground, pScreen );
	apply_image( 0, 150, pMessage, pScreen );

	/* Update the screen. */
	if( -1 == SDL_Flip( pScreen ) )
	{
		/* QUIT. */
		return 1;
	}

	/* While the user has not pressed X. */
	while( false == bQuit )
	{
		/* While there is an event to handle. */
		while( SDL_PollEvent( &event ) )
		{
			/* Check to see if the user has Xed out the window. */
			if( SDL_QUIT == event.type)
			{
				/* Quit the program. */
				bQuit = true;
			}
		}
	}

	/* Lastly before ending the program, free all the variables. */
	clean_up();

	/* If everything went as expected, return 0. */
	return 0;
}

